const express = require("express");
const mysql = require("mysql2");

const state = require("../state.js");
console.log(state.login);

const options = {
  host: "localhost",
  port: 3306,
  user: "root",
  password: "123456789",
  database: "oppo",
};

// 根据数据库配置创建数据库连接
const connection = mysql.createConnection(options);
connection.connect(); // 连接到数据库服务器

const router = express.Router();

router.get("/user/index", (req, resp) => {
  resp.render("user/index");
});


router.get("/user/sign/in", (req, resp) => {
  resp.render("user/sign-in");
});

//注册时检查用户名是否可用
router.post("/user/check/username", (req, resp) => {
  let username = req.body.username;
  let sql = `select * from t_users`;
  let handler = function (error, results, fields) {
    if (error) {
      console.log(error);
    }
    //console.log(results);
    let condition = function (x) {
      return x.username === username;
    };
    let result = results.find(condition);
    let m;
    if (result) {
      m = { state: "fail", error: "用户名已存在" };
    } else {
      m = { state: "success", error: "用户名可用" };
    }
    resp.json(m);
  };
  connection.query(sql, handler);
});

router.post("/user/sign/in", (req, resp) => {
  let uname = req.body.username;
  let passwd = req.body.password;
  let p = req.body;
  // 数据校验
  if (!uname || !passwd) {
    resp.render("user/sign-in", { error: "账号或密码不能为空" });
    return;
  }
  let sql = `select * from t_users where username='${uname}'`;
  let handler = function (error, results, fields) {
    if (error) {
      console.log(error);
      throw error;
    }
    //console.log(results);
    let result = results[0];
    if (uname == result.username) {
      if (passwd == result.password) {
         //将用户信息保存到会话(session)对象中
        req.session.username=uname;

        // 更改登录状态
        state.login = true;
        // 将已登录的教师信息保存到 state 中
        state.user = { username: uname, password: passwd };
        console.log(state.login);

        // resp.send(`<h2>${username}登录成功</h2>`);
        resp.render("user/index", { message: uname });
      } else {
        resp.render("user/sign-in", { error: "密码输入错误" });
      }
    } else {
      resp.render("user/sign-in", { error: "用户名不存在,请先注册" });
    }
  };
  connection.query(sql, handler);
});

router.get("/user/sign/up", (req, resp) => {
  resp.render("user/sign-up");
});

router.post("/user/sign/up", (req, resp) => {
  let username = req.body.username;
  let password = req.body.password;
  let confirm = req.body.confirm;
  if (!username) {
    // 将来在 sign-in.njk 文件上可以通过 {{error}} 获得 data 中的 error 属性值
    let data = { error: "用户名不可以为空哦" };
    // 渲染视图并为模板文件传入数据
    resp.render("user/sign-up", data);
    return;
  }

  if (!password) {
    let data = { error: "密码不可以为空哦" };
    resp.render("user/sign-up", data);
    return;
  }
  if (!confirm) {
    let data = { error: "确认密码不可以为空哦" };
    resp.render("user/sign-up", data);
    return;
  }

  if (password != confirm) {
    let data = { error: "两次输入的密码不一致" };
    resp.render("user/sign-up", data);
    return;
  }

  let sql = `insert into t_users (username,password) values ('${username}','${password}')`;

  let handler = function (error, results, fields) {
    if (error) {
      console.log("注册失败");
      throw error;
    }
    resp.render("user/sign-in", { message: "注册成功！现在可以登录了" });
  };
  connection.query(sql, handler);
});



module.exports = router;
